package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.servercenter.GameEditLimit;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.ForbiddenMgr;
import com.yanqu.road.server.manger.GameEditLimitMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.config.UnionConfigMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.Objects;


@Cmd(code = Protocol.S_UNION_MODIFY, desc = "修改联盟信息")
public class UnionModifyCmd extends AbstractCommand {
    private int type;         //0：联盟名称； 1：微信号； 2：qq号； 3：对外公告； 4：对内公告
    private String content;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionProto.UnionModifyReqMsg reqMsg = UnionProto.UnionModifyReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        content = reqMsg.getContent();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        //编辑限制
        GameEditLimit gameEditLimit = GameEditLimitMgr.getGameEditLimit(Protocol.S_UNION_MODIFY);
        if(null != gameEditLimit){
            long nowTime = System.currentTimeMillis();
            if(nowTime >= gameEditLimit.getLimitEditStartTime() && nowTime < gameEditLimit.getLimitEditEndTime()){
                ret = GameErrorCode.E_SYSTEM_NO_OPEN;
                return;
            }
        }

        String unionUid = player.getUserInfo().getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }
        UnionInfo unionInfo = CrossUnionMgr.getUnionInfo(unionUid);

        if (type == 0 || type == 3) {
            /**
             * 送财功能被禁用 则返回系统未开放
             */
            if(ForbiddenMgr.isForbiddenCmd(player.getUserId(), Protocol.S_UNION_MODIFY)) {
                ret = GameErrorCode.E_SYSTEM_NO_OPEN;
                return;
            }
        }

        int channelId = ServerListMgr.getChannelId(GameServer.getInstance().getServerId());
        if((0 == type || 3 == type) && //改商会名或者改商会公告
                eChannelType.isMainLandChannel(channelId) && ChannelConfig.CN_FORBIDDEN_WORD_CHECK == 1) {//大陆渠道，实时检测内容是否屏蔽
            //PlatformMgr.addUnionModifyTask(player, type, content);
            GameServer.getSqlCommandExecutor().execute(() -> {

                int ret;
                if (0 == type) {//商会名
                    ret = UnionConfigMgr.checkUnionName(content, unionUid, player);
                } else if (3 == type) {//对外公告
                    ret = UnionConfigMgr.checkOutNotice(content, unionUid, player);
                } else {
                    ret = GameErrorCode.E_UNION_MODIFY_TYPE_ERROR;
                }
                if (0 != ret) {
                    UnionProto.UnionModifyRespMsg.Builder respMsg = UnionProto.UnionModifyRespMsg.newBuilder();
                    respMsg.setRet(ret);
                    player.sendPacket(Protocol.U_UNION_MODIFY, respMsg);

                }else {

                    //原本逻辑
                    CrossUnionProto.UnionModifyCrossReqMsg.Builder crossReq = CrossUnionProto.UnionModifyCrossReqMsg.newBuilder();
                    crossReq.setContent(content);
                    crossReq.setType(type);
                    crossReq.setConsume("");
                    //判定是否需要扣除元宝
                    if(type == 0){

                        if (Objects.equals(content, unionInfo.getUnionName())) {
                            UnionProto.UnionModifyRespMsg.Builder respMsg = UnionProto.UnionModifyRespMsg.newBuilder();
                            respMsg.setRet(GameErrorCode.E_UNION_NAME_SAME);
                            player.sendPacket(Protocol.U_UNION_MODIFY, respMsg);
                            return;
                        }

                        //改名需要消耗元宝
                        Property property = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(GameConfig.UNION_CHANGE_NAME_COST));
                        if (!player.getModule(CurrencyModule.class).currencyIsEnough(property)) {
                            UnionProto.UnionModifyRespMsg.Builder respMsg = UnionProto.UnionModifyRespMsg.newBuilder();
                            respMsg.setRet(GameErrorCode.E_BAG_INGOTS_NO_ENOUGH);
                            player.sendPacket(Protocol.U_UNION_MODIFY, respMsg);
                            return;
                        }
                        crossReq.setConsume(PropertyHelper.parsePropertyToString(property));
                    }

                    //直接上跨服
                    player.sendPacket(Protocol.C_UNION_MODIFY, crossReq);

                }
            });


        }else {

            if (0 == type) {//商会名
                ret = UnionConfigMgr.checkUnionName(content, unionUid, player);
            } else if (1 == type) {//微信号
                ret = UnionConfigMgr.checkWechatCode(content);
            } else if (2 == type) {//qq号
                ret = UnionConfigMgr.checkQqCode(content);
            } else if (3 == type) {//对外公告
                ret = UnionConfigMgr.checkOutNotice(content, unionUid, player);
            } else if (4 == type) {//对内公告
                ret = UnionConfigMgr.checkInnerNotice(content);
            } else {
                ret = GameErrorCode.E_UNION_MODIFY_TYPE_ERROR;
            }
            if (0 != ret) {
                return;
            }

            //原本逻辑
            CrossUnionProto.UnionModifyCrossReqMsg.Builder crossReq = CrossUnionProto.UnionModifyCrossReqMsg.newBuilder();
            crossReq.setContent(content);
            crossReq.setType(type);
            crossReq.setConsume("");
            //判定是否需要扣除元宝
            if(type == 0){

                if (Objects.equals(content, unionInfo.getUnionName())) {
                    ret = GameErrorCode.E_UNION_NAME_SAME;
                    return;
                }

                //改名需要消耗元宝
                Property property = new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(GameConfig.UNION_CHANGE_NAME_COST));
                if (!player.getModule(CurrencyModule.class).currencyIsEnough(property)) {
                    ret = GameErrorCode.E_BAG_INGOTS_NO_ENOUGH;
                    return;
                }
                crossReq.setConsume(PropertyHelper.parsePropertyToString(property));
            }

            //直接上跨服
            player.sendPacket(Protocol.C_UNION_MODIFY, crossReq);
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            UnionProto.UnionModifyRespMsg.Builder respMsg = UnionProto.UnionModifyRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_UNION_MODIFY, respMsg);
        }
    }
}
